*********************************************************************************************************
*************    Replication code for IMF Lending Programs and Repression in Autocracies  ***************
*********************************************************************************************************

***Analyses run in Stata 14.2***

*** Note that the following Stata packages need to be installed before replicating tables and figures reported in "IMF Lending Programs and Repression in Autocracies": ivreg2, reghdfe, ivreghdfe, coefplot, cmp, ghk2, estout ***

clear
* Install ftools 
cap ado uninstall ftools
net install ftools, replace from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")

* Install reghdfe
cap ado uninstall reghdfe
net install reghdfe, replace from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")

* Install ivreg2
cap ado uninstall ivreg2
ssc install ivreg2, replace

* Install ivreghdfe
cap ado uninstall ivreghdfe
net install ivreghdfe, replace from(https://raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/)

*Install estout*
ssc install estout, replace

*Install coefplot*
ssc install coefplot, replace

*Install cmp*
ssc install cmp, replace

*Install ghk2*
ssc install ghk2, replace


*******************************************************************************************************************
******** REPRODUCE FIGURE 1 (SHARES OF DEMOCRATIC AND NON-DEMOCRATIC COUNTRIES INVOLVED IN IMF PROGRAMS) **********
*******************************************************************************************************************

clear
cd "/Users/stephennelson/Desktop/Aug24ISQReplication"
use "figure1data_ISQ.dta"

*create yearly IMF program participation average for democracies*
egen imf_avg_democ = mean(imfprogram_uu) if e_chga_demo_updated==1, by(year)

*create IMF program participation average for non-democracies*
egen imf_avg_autoc = mean(imfprogram_uu) if e_chga_demo_updated==0, by(year)

*Figure 1 in main paper*
twoway (line imf_avg_democ imf_avg_autoc year if year>=1972 & year<=2014, sort msymbol(d t) connect (l l) lwidth (medthin thin) ytitle("Average under IMF Program", axis(1)) xlab(1973(5)2014) xtitle("") legend(cols(1) order(1 2) lab(1 "Democratic Countries") lab(2 "Non-Democratic Countries") stack) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)))

clear 

*****************************************************************************************************************************************
******** STATISTICAL ANALYSES OF PHYS. INTEGRITY RIGHTS-IMF PROGRAM PARTICIPATION LINK (ONLY DEVELOPING DEMOCRATIC REGIMES) *************
*****************************************************************************************************************************************

cd "/Users/stephennelson/Desktop/Aug24ISQReplication"
use "democracies_IMFprograms_ISQ.dta"

set matsize 800
tsset ccode year, yearly

***** Figure 3 (in main paper) / Table A7 (in supplementary appendix) *****
* Table A7 column 1 *
ivreg2 theta_mean l1.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode if year>1974 & year<2015 & advanced==0, robust   
est store fariss
qui tab ccode if e(sample)
di r(r)
* Table A7 column 2 *
ivreg2 v2x_clphy l1.v2x_clphy imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode if year>1974 & year<2015 & advanced==0, robust 
est store vdem
qui tab ccode if e(sample)
di r(r)

* Tables A3 and A4 (descriptive stats and pairwise correlations for developing democracies sample) *
qui ivreg2 theta_mean l1.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode if year>1974 & year<2015 & advanced==0, robust   
sum theta_mean v2x_clphy imf_maxlog_liquidity_1 imf_movingavg1  e_migdppcln v2xcl_acjst v2exl_legitideol actotal if e(sample)  
corr theta_mean imf_maxlog_liquidity_1 imf_movingavg1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal if e(sample)  

* Figure 3 in main paper *
coefplot fariss vdem, keep(l1.theta_mean l1.v2x_clphy imfprogram_uu imf_movingavg1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal) coeflabel(l1.theta_mean = "Lagged HR Measure" l1.v2x_clphy = "Lagged HR Measure" imfprogram_uu = "IMF Program Participation (Inst.)" imf_movingavg1 = "Moving IMF Program Partic. Avg." e_migdppcln = "Log  of Per Capita GDP"  v2xcl_acjst = "Access to Justice Index" v2exl_legitideol = "Ideological Commitment" actotal = "Magnitude of War Intensity") xline(0) xtitle(Coefficient and 95% Confidence Interval) xlab(-.6(.1).5) legend(off) ciopts(pstyle(p4)) mcolor(black black black black black black black) msize(small) mlabposition(1) mlabgap(*0.1) mlabel(cond(@pval<.01, "***",cond(@pval<.05, "**", cond(@pval<.1, "*",  cond(@pval<.15, "", ""))))) note(" * p < .1, ** p < .05, *** p < .01") scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) 

clear 

*************************************************************************************************************************************
******** STATISTICAL ANALYSES OF PHYS. INTEGRITY RIGHTS-IMF PROGRAM PARTICIPATION LINK (ONLY AUTOCRATIC REGIMES) ********************
*************************************************************************************************************************************

cd "/Users/stephennelson/Desktop/Aug24ISQReplication"
use "autocracies_IMFprograms_ISQ.dta"

set matsize 800
tsset ccode year, yearly

***** Figure 2 (in main paper) *****
gen imf_above_median = 1 if imf_avg_1970_2014>.377 & imf_avg_1970_2014 !=.
recode imf_above_median . = 0
gen imf_below_median = 1 if imf_avg_1970_2014<.377 & imf_avg_1970_2014 !=.
recode imf_below_median . = 0
gen imf_zero = 1 if imf_avg_1970_2014==0
egen theta_mean_zero = mean(theta_mean) if imf_zero==1, by(year)
egen theta_mean_above = mean(theta_mean) if imf_above_median==1, by(year)
egen theta_mean_below = mean(theta_mean) if imf_below_median==1, by(year)
twoway (line theta_mean_above theta_mean_below theta_mean_zero year, yaxis(1) sort msymbol(d t p) connect (l l l) lwidth (medthin thin vthin) ytitle("Phys. Integrity Rights Avg.", axis(1)) xlab(1973(10)2013) xtitle("")) (line log_imf_liquidity_ratio year if ccode==41, yaxis(2) sort msymbol(-) connect (l) ytitle("IMF Liquidity Ratio", axis(2)) xlab(1973(10)2013) xtitle("") legend(cols(1) order(1 2 3 4) lab(1 "Phys. Integrity Rights Avg. (high IMF probability)") lab(2 "Phys. Integrity Rights Avg. (low IMF probability)") lab (3 "Phys. Integrity Rights Avg. (zero IMF probability)") lab (4 "IMF Liquidity") stack) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)))

***** Table 1 (in main paper) *****
* Col 1 *
ivreg2 theta_mean l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
qui tab country if e(sample)
di r(r)
* Col 2 *
ivreg2 theta_mean l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal debt_crisis ungaidealpoint2 i.year i.ccode, robust 
qui tab country if e(sample)
di r(r)

*** Table A1 (summary stats) & Table A2 (pairwise correlations) in appendix ***
qui ivreg2 theta_mean l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
sum theta_mean imf_maxlog_liquidity_1 imf_movingavg1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal if e(sample)
corr theta_mean imf_maxlog_liquidity_1 imf_movingavg1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal if e(sample)

*** Table A8 (2SLS results for autocratic sample, with first stage) ***
xi: ivreg2 theta_mean l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.ccode i.year, robust first
xi: ivreg2 theta_mean l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal debt_crisis ungaidealpoint2 i.ccode i.year, robust first

***** Figure 4 in main paper and Table A9 (lags of compound instrument up to t-7) *****
ivreg2 theta_mean l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag1
ivreg2 theta_mean l.theta_mean l2.imf_movingavg1 (l2.imfprogram_uu= l2.imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag2
ivreg2 theta_mean l.theta_mean l3.imf_movingavg1 (l3.imfprogram_uu= l3.imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag3
ivreg2 theta_mean l.theta_mean l4.imf_movingavg1 (l4.imfprogram_uu= l4.imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag4
ivreg2 theta_mean l.theta_mean l5.imf_movingavg1 (l5.imfprogram_uu= l5.imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag5
ivreg2 theta_mean l.theta_mean l6.imf_movingavg1 (l6.imfprogram_uu= l6.imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag6
ivreg2 theta_mean l.theta_mean l7.imf_movingavg1 (l7.imfprogram_uu= l7.imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
est store lag7
coefplot lag1 lag2 lag3 lag4 lag5 lag6 lag7, keep(imfprogram_uu L2.imfprogram_uu L3.imfprogram_uu L4.imfprogram_uu L5.imfprogram_uu L6.imfprogram_uu L7.imfprogram_uu) coeflabel(imfprogram_uu = "IMF Program Participation (t-1)" L2.imfprogram_uu = "IMF Program Participation (t-2)" L3.imfprogram_uu = "IMF Program Participation (t-3)" L4.imfprogram_uu = "IMF Program Participation (t-4)" L5.imfprogram_uu = "IMF Program Participation (t-5)" L6.imfprogram_uu = "IMF Program Participation (t-6)" L7.imfprogram_uu = "IMF Program Participation (t-7)") xlab(-.6(.1).1) xline(0) xtitle(Coefficient and 95% Confidence Interval) legend(off) ciopts(pstyle(p4)) mcolor(black black black black black black black) msymbol(o o o o o o o) msize(small) mlabposition(1) mlabgap(*0.1) mlabel(cond(@pval<.01, "***",cond(@pval<.05, "**", cond(@pval<.1, "*",  cond(@pval<.15, "", ""))))) note(" * p < .1, ** p < .05, *** p < .01") scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white))

*** Table A10 (2SLS results for autocratic sample, with HAC errors) ***
ivreghdfe theta_mean l.theta_mean (imfprogram_uu= imf_maxlog_liquidity_1) imf_movingavg1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal, absorb (ccode year)rob bw(2)gmm2s ffirst  
ivreghdfe theta_mean l.theta_mean (imfprogram_uu= imf_maxlog_liquidity_1) imf_movingavg1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal debt_crisis ungaidealpoint2 , absorb (ccode year)rob bw(2)gmm2s ffirst  

*** Table A11 (2SLS results for autocratic sample, with additional covariates) ***
* Col 1 adds economic liberalization *
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal elc debt_crisis ungaidealpoint2 i.year i.ccode, robust
* Col 2 adds logged population size *
gen log_e_wb_pop = log(e_wb_pop)
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal log_e_wb_pop debt_crisis ungaidealpoint2 i.year i.ccode, robust
* Col 3 adds V-Dem mass mobilization index *
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal v2cagenmob debt_crisis ungaidealpoint2 i.year i.ccode, robust 
* Col 4 adds state capacity *
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal capacity debt_crisis ungaidealpoint2 i.year i.ccode, robust
* Col 5 adds coups *
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal e_pt_coup debt_crisis ungaidealpoint2 i.year i.ccode, robust
* Col 6 adds corruption *
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal v2x_corr  debt_crisis ungaidealpoint2 i.year i.ccode, robust
* Col 7 adds military, monarchical, and party-based autocratic varieties *
ivreg2 theta_mean  l.theta_mean imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol actotal military monarchy party debt_crisis ungaidealpoint2 i.year i.ccode, robust

*** Table A12 (2SLS results for autocratic sample with V-Dem physical violence indicator as outcome variable ***
ivreg2 v2x_clphy l.v2x_clphy imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal i.year i.ccode, robust 
ivreg2 v2x_clphy l.v2x_clphy imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1) e_migdppcln v2xcl_acjst v2exl_legitideol actotal debt_crisis ungaidealpoint2 i.year i.ccode, robust 
  
*** Table A13 (placebo-style test, 2SLS results for autocratic sample with non-state violence as outcome variable) ***
ivreg2 v2caviol  l.v2caviol imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol v2cagenmob i.year i.ccode, robust
ivreg2 v2caviol  l.v2caviol imf_movingavg1 (imfprogram_uu= imf_maxlog_liquidity_1)  e_migdppcln v2xcl_acjst v2exl_legitideol v2cagenmob debt_crisis ungaidealpoint2 i.year i.ccode, robust

clear 

********************************************************************************************************************************
******** STATISTICAL ANALYSES OF PHYS. INTEGRITY RIGHTS-IMF PROGRAM CONDITIONALITY LINK (AUTOCRATIC REGIMES) *******************
********************************************************************************************************************************

cd "/Users/stephennelson/Desktop/Aug24ISQReplication"
use "imf_conditions_ISQ.dta"

sort cid time
xtset cid time

***** Table 2 (in main paper) *****
* Predictors of IMF program participation (based on Reinsberg et al. 2019a) and main correlates of human rights scores *  
global Z1  limf1 nunder s_unga3g7
global Z2  $Z1 reserves_wdi debtser_gni cab_gdp dpi_exelec dpi_legelec
global BMA1 e_migdppcln v2xcl_acjst v2exl_legitideol actotal

* Test for effect of dichotomous IMF program participation indicator (not reported in paper) *
qui treatreg theta_mean imf1 l.theta_mean i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
est store a1
drop imr
qui treatreg theta_mean imf1 l.theta_mean i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
est store a2
drop imr
estout a1 a2, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N chi2, fmt(%7.0f %7.2f)) drop(*cid* *time* _cons) order(imf1)

* Distinguishing structural and stabilization conditions (col 1) *
cmp setup 
set matsize 800 
qui xi:reg theta_mean i.cid i.time  
qui treatreg theta_mean imf1 l.theta_mean i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
qui cmp (theta_mean=scb qcb imr l.theta_mean _Icid* _Itime*) (scb=ivscb _Icid* _Itime*) (qcb=ivqcb _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid)
est store e1
test ([scb]ivscb [qcb]ivqcb)
drop imr 
estout e1, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 chi2 ll, fmt(%7.0f %7.2f)) drop(*cid* *time* _cons)

* Different types of IMF program conditions, labor-related and privatization (col 2 & 3) *
qui treatreg theta_mean imf1 l.theta_mean $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
qui cmp (theta_mean=ba2lab imr l.theta_mean $BMA1 _Icid* _Itime*) (ba2lab=ivba2lab $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
est store f1
test ([ba2lab]ivba2lab)
drop imr
qui treatreg theta_mean imf1 l.theta_mean $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
qui cmp (theta_mean=ba2pri imr l.theta_mean $BMA1 _Icid* _Itime*) (ba2pri=ivba2pri $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
est store f2
test ([ba2pri]ivba2pri)
drop imr
estout f1 f2, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) drop(*cid* *time* _cons)
qui tab cid if e(sample)
di r(r)

*Tables A5 & A6 (summary statistics and pairwise correlations)*
sum theta_mean scb qcb ba2lab ba2pri e_migdppcln v2xcl_acjst v2exl_legitideol actotal if e(sample)  
corr theta_mean scb qcb ba2lab ba2pri e_migdppcln v2xcl_acjst v2exl_legitideol actotal if e(sample)

clear
